package src.leetcode.everyday.hard_2931;

import java.util.Comparator;
import java.util.*;

/**
 * @author liyizzz
 * @description 购买物品的最大开销
 * @date 2024/12/12 14:25
 */
public class maxSpending {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.maxSpending(new int[][]{{8,5,2}, {6,4,1}, {9,7,3}}));
    }
}
class Solution {

    public long maxSpending(int[][] values) {
        PriorityQueue<Pair> que = new PriorityQueue<Pair>(Comparator.comparing(p -> p.value));
        int n = values.length;
        int m = values[0].length;
        for (int i = 0; i < n; i++) {
            que.add(new Pair(i, m - 1, values[i][m - 1]));
        }

        long ans = 0L;
        long day = 1L;
        while (!que.isEmpty()) {
            Pair temp = que.poll();
            ans += day * temp.value;
            day++;
            if (temp.produceIndex > 0) {
                temp.produceIndex = temp.produceIndex - 1;
                temp.value = values[temp.storeIndex][temp.produceIndex];
                que.add(temp);
            }
        }
        return ans;
    }

    class Pair {
        public int storeIndex;
        public int produceIndex;
        public int value;
        Pair(int storeIndex, int produceIndex, int value) {
            this.storeIndex = storeIndex;
            this.produceIndex = produceIndex;
            this.value = value;
        }
    }
}